package com.bite.demo.mapper;


import com.bite.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    /**
     * 结果映射
     * 方法一: 对字段进行重命名
     * @return
     */
    @Select("select id, username, password, age, gender, phone," +
            " delete_flag as deleteFlag, create_time as createTime, update_time as updateTime" +
            " from userinfo")
    List<UserInfo> selectAll();

    /**
     * 结果映射
     * 方法二: 使用注解
     * @return
     */
    @Results(id = "BaseMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    @Select("select * from userinfo")
    List<UserInfo> selectAll2();

    /**
     * 结果映射
     * 方法三: 使用配置的方式, 自动转驼峰
     * @return
     */
    @Select("select * from userinfo")
    List<UserInfo> selectAll3();


    @ResultMap(value = "BaseMap")
    @Select("select * from userinfo where id= '${id}'")
    UserInfo selectOne(Integer id);

    @ResultMap(value = "BaseMap")
    @Select("select * from userinfo where username= #{username}")
    List<UserInfo> selectByName(String username);

    @Select("select * from userinfo where id= #{userId}")
    UserInfo selectOne2(@Param("userId") Integer id);

    //返回自增ID
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender,phone) " +
            "values(#{username},#{password},#{age},#{gender},#{phone})")
    Integer insert(UserInfo userInfo);

    //对Insert的参数进行重命名
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender,phone) " +
            "values(#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender},#{userInfo.phone})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from userinfo where id=#{id}")
    Integer delete(Integer id);

    @Update("update userinfo set age=#{age} where id=#{id}")
    Integer update(UserInfo userInfo);

    /**
     * 排序
     * @param sort
     * @return
     */
    @Select("select * from userinfo order by id ${sort}")
    List<UserInfo> selectUserBySort(String sort);

    /**
     * 模糊查询
     * @param username
     * @return
     */
    @Select("select * from userinfo where username like CONCAT('%',#{username},'%')")
    List<UserInfo> selectUserByLike(String username);

}
